﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for UsersDAL
/// </summary>
public class UsersDAL: StandardConnectDB, IUsers
{
	public UsersDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public List<Users> GetAllUsers()
    {
        //Create object listUsers
        List<Users> listUsers = new List<Users>();

        //Get all Users from database
        DataTable dt = this.DTExecuteSelect("SELECT * FROM Users u join ROLES r on u.IdRole =r.IdRole");

        //Add Users from DataTable to listUsers
        foreach (DataRow dr in dt.Rows)
        {
            //Convert objec DataRow to object Users and add to list users
            listUsers.Add(this.ConvertObjectDataRowToUser(dr));
        }
                
        return listUsers;
    }

    public List<Users> GetUsersByRole(Roles role)
    {
        //Create object listUsers
        List<Users> listUsers = new List<Users>();

        //Create object paramList store parameter execute store procedure
        Dictionary<String, Object> paramList = new Dictionary<String, Object>();

        //Add parameter pIdRole to paramList
        paramList.Add("@pIdRole", role.IdRole);

        //Get Users by store procedure
        DataTable dt = this.DTExecuteSelect("procGetUserByIdRole", paramList);

        //Add Users from DataTable to listUsers
        foreach (DataRow dr in dt.Rows)
        {
            //Convert objec DataRow to object Users and add to list users
            listUsers.Add(this.ConvertObjectDataRowToUser(dr));
        }

        return listUsers;
    }

    public Users GetUserByUsername(string username)
    {
        //Create object Users
        Users user = new Users();

        //Create object paramList store parameter execute store procedure
        Dictionary<string, object> paramList = new Dictionary<string, object>();

        //Add parameter pUsername to paramList
        paramList.Add("@pUsername", username);

        //Get Users by store procedure
        DataTable dt = this.DTExecuteSelect("procGetUserByUsername", paramList);
        if (dt.Rows.Count != 0)
        {
            //Create object DataRow store data first's row in DataTable
            DataRow dr = dt.Rows[0];

            //Convert object DataRow to object Roles
            user = this.ConvertObjectDataRowToUser(dr);

            return user;
        }
        else
        {
            return null;
        }
    }

    public bool CreateUser(string username, string password, string email)
    {
        bool result = false;

        //Create param list store parameter execute store procedure
        Dictionary<string, object> paramList = new Dictionary<string, object>();
        //Add parameter to paramList
        paramList.Add("@pUsername", username);
        paramList.Add("@pPassword", password);
        paramList.Add("@pEmail", email);
       
        //Insert object User to table Users in database
        result = this.ExecuteUpdate("procCreateUser", paramList);

        return result;
    }

    public bool UpdateUser(Users user)
    {
        bool result = false;

        //Create param list store parameter execute store procedure
        Dictionary<string, object> paramList = new Dictionary<string, object>();
        //Add parameter to paramList
        paramList.Add("@pUsername", user.Username);
        paramList.Add("@pPassword", user.Password);
        paramList.Add("@pActive", user.Active);
        paramList.Add("@pIdRole", user.Role.IdRole);

        //Update object User for table Users in database
        result = this.ExecuteUpdate("procUpdateUser", paramList);

        return result;
    }

    public Users CheckLogin(string username, string password)
    {
        //Create Object User
        Users user = new Users();

        //Create object paramList store parameter execute store procedure
        Dictionary<String, Object> paramList = new Dictionary<String, Object>();

        //Add two parameter pUsername and pPassword to paramList
        paramList.Add("@pUsername", username);
        paramList.Add("@pPassword", password);

        //Get User by store procedure
        DataTable dtUsers = this.DTExecuteSelect("procCheckLogin", paramList);

        if (dtUsers.Rows.Count != 0)
        {
            //Create DataRow store datas of User from DataTable
            DataRow dr = dtUsers.Rows[0];

            //Conver object DataRow to object Users
            user = this.ConvertObjectDataRowToUser(dr);
            return user;
        }
        return null;
    }

    public Users ConvertObjectDataRowToUser(DataRow dr)
    {
        //Create object Users
        Users user = new Users();

        //Set properties of Users
        user.Username = dr["Username"].ToString();
        user.Password = dr["Password"].ToString();
        user.Email = dr["Email"].ToString();
        user.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
        user.Active = bool.Parse(dr["Active"].ToString());

        //Get idRole
        int idRole = int.Parse(dr["IdRole"].ToString());
        //Create object IRoles
        IRoles iRole = new RoleDAL();
        //Set property IdRole of Users
        user.Role = iRole.GetRoleById(idRole);

        return user;
    }
}